<template>
  <div class="app-container">
    <div class="filter-container">
      <el-row>
        <el-col :span="24">
          <el-input
            placeholder="请输入账号"
            v-model="listQuery.account"
            style="width: 200px"
            class="filter-item"
            clearable
            @keyup.enter.native="handleFilter"
          />
          <el-select
            v-model="listQuery.state"
            placeholder="账号状态"
            clearable
            style="width: 200px"
            class="filter-item"
          >
            <el-option
              v-for="item in stateOptions"
              :key="'state' + item.key"
              :label="item.name"
              :value="item.key"
            />
          </el-select>
          <el-select
            v-model="listQuery.is_admin"
            placeholder="权限类型"
            clearable
            class="filter-item"
            style="width: 200px"
          >
            <el-option
              v-for="item in isAdminOptions"
              :key="'is_admin' + item.key"
              :label="item.name"
              :value="item.key"
            />
          </el-select>
          <el-button
            class="filter-item"
            type="primary"
            icon="el-icon-search"
            @click="handleFilter"
            >查询</el-button
          >
        </el-col>
      </el-row>
    </div>
    <div class="options-container">
      <el-button
        class="filter-item"
        style="margin-left: 10px"
        type="primary"
        icon="el-icon-edit"
        @click="showUserAdd()"
        >添加</el-button
      >
    </div>
    <div class="table-container">
      <el-table
        v-loading="listLoading"
        :key="tableKey"
        :data="list"
        border
        fit
        @sort-change="sortHanlde"
        highlight-current-row
        style="width: 100%"
      >
        <el-table-column
          label="序号"
          prop="id"
          sortable="custom"
          align="left"
          width="105"
        >
        </el-table-column>
        <el-table-column
          label="账号"
          prop="account"
          sortable="custom"
          width="150px"
          align="left"
        >
        </el-table-column>
        <el-table-column
          label="动态随机码"
          :show-overflow-tooltip="true"
          prop="token"
          width="180px"
        >
        </el-table-column>
        <el-table-column label="状态" width="110px" align="center">
          <template slot-scope="scope">
            <span>{{ stateOptionsObj[scope.row.state] }}</span>
          </template>
        </el-table-column>
        <el-table-column label="账号类型" width="110px" align="center">
          <template slot-scope="scope">
            <span>{{ isAdminOptionsObj[scope.row.is_admin] }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="创建时间"
          prop="create_time"
          sortable="custom"
          width="160px"
          align="center"
        >
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          width="300"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-tooltip
              class="item"
              effect="dark"
              content="绑定角色"
              placement="top-start"
            >
              <el-button
                type="primary"
                size="mini"
                @click="bindRoles(scope.row)"
                >角色</el-button
              >
            </el-tooltip>
            <el-button type="primary" size="mini" @click="privilege(scope.row)"
              >特权</el-button
            >
            <el-tooltip
              class="item"
              effect="dark"
              content="初始化该账号密码"
              placement="top-start"
            >
              <el-button type="success" size="mini" @click="resetpwd(scope.row)"
                >密码</el-button
              >
            </el-tooltip>

            <el-button
              v-if="scope.row.state == 1 || scope.row.state == 0"
              type="danger"
              size="mini"
              @click="removeUser(scope.row)"
              >删除
            </el-button>
            <el-button
              v-else
              type="warning"
              size="mini"
              @click="recovery(scope.row)"
              >恢复
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="pageHandle"
      />
    </div>

    <div class="dialog-container">
      <userResetPwdctl :show.sync="userResetPwd.show" :data="userResetPwd.data">
      </userResetPwdctl>
      <userAddctl :show.sync="userAdd.show" @success="getList"> </userAddctl>
      <userAuthctl
        :show.sync="userAuth.show"
        :user_id="userAuth.user_id"
        :user_name="userAuth.user_name"
      >
      </userAuthctl>
      <userBindRolesctl
        :show.sync="userBindRoles.show"
        :user_id="userBindRoles.user_id"
        :user_name="userBindRoles.user_name"
      >
      </userBindRolesctl>
    </div>
  </div>
</template>

<script>
import {
  users_getlist,
  stateOptions,
  isAdminOptions,
  remove_by_id,
  recovery,
} from "@/api/right";
import Pagination from "@/components/Pagination"; // Secondary package based on el-pagination
import userResetPwdctl from "./components/userResetPwd";
import userAddctl from "./components/userAdd";
import userAuthctl from "./components/userAuth";
import userBindRolesctl from "./components/userBindRoles";

export default {
  name: "userManage",
  components: {
    Pagination,
    userResetPwdctl,
    userAddctl,
    userAuthctl,
    userBindRolesctl,
  },
  data() {
    return {
      stateOptions: [],
      isAdminOptions: [],
      stateOptionsObj: {},
      isAdminOptionsObj: {},
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        account: "",
        state: "",
        is_admin: "",
        page: 1,
        limit: 20,
        filed: "id",
        sort: "ascending",
      },
      userResetPwd: {
        show: false,
        data: undefined,
      },
      userAdd: {
        show: false,
      },
      userAuth: {
        show: false,
        user_id: 0,
        user_name: "",
      },
      userBindRoles: {
        show: false,
        user_id: 0,
        user_name: "",
      },
    };
  },
  created() {
    this.stateOptions = stateOptions;
    this.isAdminOptions = isAdminOptions;
    stateOptions.map((it) => {
      this.stateOptionsObj[it.key] = it.name;
    });
    isAdminOptions.map((it) => {
      this.isAdminOptionsObj[it.key] = it.name;
    });
    this.getList();
  },
  methods: {
    getList() {
      this.listLoading = true;
      users_getlist(this.listQuery).then((response) => {
        this.list = response.data;
        this.total = response.count || 0;
        setTimeout(() => {
          this.listLoading = false;
        }, 500);
      });
    },
    handleFilter() {
      this.getList();
    },
    sortHanlde(column) {
      console.log(column);
      this.listQuery.filed = column.prop;
      this.listQuery.sort = column.order;
      this.getList();
    },
    pageHandle(page) {
      this.listQuery.page = page.page;
      this.listQuery.limit = page.limit;
      this.getList();
    },
    removeUser(row) {
      this.$confirm(`此操作将永久删除${row.account}账号, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          remove_by_id(row.id).then((response) => {
            if (response.data > 0) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            } else {
              this.$message({
                type: "info",
                message: "删除失败",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    recovery(row) {
      this.$confirm(
        `此操作将恢复${row.account}账号正常使用, 是否继续?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          recovery(row.id).then((response) => {
            if (response.data > 0) {
              this.$message({
                type: "success",
                message: "恢复成功!",
              });
            } else {
              this.$message({
                type: "info",
                message: "恢复失败",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消恢复",
          });
        });
    },
    resetpwd(row) {
      this.userResetPwd.show = true;
      this.userResetPwd.data = row;
    },
    showUserAdd() {
      this.userAdd.show = true;
    },
    privilege(row) {
      this.userAuth.show = true;
      this.userAuth.user_id = row.id;
      this.userAuth.user_name = row.account;
    },
    bindRoles(row) {
      this.userBindRoles.show = true;
      this.userBindRoles.user_id = row.id;
      this.userBindRoles.user_name = row.account;
    },
  },
};
</script>
